package com.jc.camerascan.core.http;

import com.jc.camerascan.BuildConfig;
import com.jc.camerascan.utils.MMKVUtils;
import com.xuexiang.xhttp2.logs.HttpLog;
import com.xuexiang.xhttp2.model.HttpHeaders;
import com.xuexiang.xutil.data.DateUtils;
import com.xuexiang.xutil.security.EncryptUtils;

import java.io.IOException;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @ProjectName: PhotoRecognition
 * @Package: com.jc.camerascan.core.http
 * @ClassName: CustomDynamicInterceptor
 * @Description: java类作用描述
 * @Author: XuChi
 * @CreateDate: 2020/7/13 15:52
 * @Version: 1.0
 */
public class MyHeadersInterceptor implements Interceptor {


    @Override
    public Response intercept(Chain chain) throws IOException {
        String token = MMKVUtils.getString("xc_access_token", "");

        Request.Builder builder = chain.request().newBuilder();

//        if (headers.headersMap.isEmpty()) return chain.proceed(builder.build());

        try {
            if (token != null && !token.isEmpty()) {
                builder.addHeader("token", token);
            }
            long timestamp = DateUtils.getNowMills() / 1000;
            builder.addHeader("version", BuildConfig.VERSION_CODE + "").build();
            builder.addHeader("source", "Android").build();
            builder.addHeader("timestamp", timestamp + "").build();
            String str = "version=" + BuildConfig.VERSION_CODE + ",source=" + "Android" + ",timestamp=" + timestamp;
            builder.addHeader("sign", EncryptUtils.encryptMD5ToString(str)).build();
        } catch (Exception e) {
            HttpLog.e(e);
        }
        return chain.proceed(builder.build());

    }
}
